package user

import (
	db "tacitus/library/database"
	errs "tacitus/library/error"
)

// 用户头像库
type Head struct {
	ID int64 `json:"id" xorm:"pk autoincr"`
	Title string `json:"title"`
	File string `json:"file"`
}

func (Head) TableName() string {
	return db.TablePrefix +"user_head"
}

// 获取指定用户正在使用的头像
func GetUserHead(uid int64) (*Head, error) {
	record := new(User)
	if ok, err := x.Where("id = ?", uid).Get(record); err != nil {
		return nil, err
	} else if !ok {
		record.HeadID = 1
	}
	head := new(Head)
	if ok, err := x.ID(record.HeadID).Get(head); err != nil {
		return nil, err
	} else if !ok {
		return nil, errs.Called("头像不存在")
	}
	return head, nil
}

func GetUserHeadFile(id int64) string {
	head := new(Head)
	if ok, err := x.ID(id).Get(head); err != nil {
		return ""
	} else if !ok {
		return ""
	}
	return head.File
}

func MatchUserHeadFile(ids []int64) (map[int64]string, error) {
	data := make([]Head, 0)
	if err := x.In("id", ids).Find(&data); err != nil {
		return nil, err
	}
	m := make(map[int64]string)
	for _, item := range data {
		m[item.ID] = item.File
	}
	return m, nil
}